//
//  WmcAddCustomAddressCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/8/9.
//

import UIKit

class WmcAddCustomAddressCell: WmcBaseTableCell {
    // Internal

    /// 设置/取消常用地址
    var setCustomAddress: ((Int) -> Void)?

    override func makeUI() {
        contentView.addSubview(addressTitleLabel)
        contentView.addSubview(descLabel)
        contentView.addSubview(addBtn)
        contentView.addSubview(line)

        addressTitleLabel.snp.makeConstraints { make in
            make.left.top.equalTo(16)
            make.right.equalTo(-90)
        }

        descLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.bottom.equalTo(-16)
            make.right.equalTo(-90)
        }

        addBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
            make.width.equalTo(78)
            make.height.equalTo(32)
        }
 
        line.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
        }

        addBtn.addTarget(self, action: #selector(customAction(_:)), for: .touchUpInside)
    }

    func bindModel(_ model: WmcSelectAddressModel, index: Int) {
        self.model = model
        self.index = index
        
        self.addressTitleLabel.text = model.poi.name
        
        let color = model.isSelected ? UIColor.wmc_mainTheme : UIColor.wmc_grayH3_505050
        addressTitleLabel.textColor = color
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    // Private

    private var index: Int = 0
    private var model: WmcSelectAddressModel?
    private var addressTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_grayH3_505050
        label.font = 16.wmc_font
        return label
    }()

    private var descLabel: UILabel = {
        let label = UILabel()
        label.text = "+添加新地点"
        label.textColor = .wmc_grayH12_A0A0A0
        label.font = 12.wmc_font
        return label
    }()

    private var commonlyIconView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH16_EBEBEB
        view.layer.cornerRadius = 2
        return view
    }()

    private var addBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("+添加地点", for: .normal)
        btn.setTitleColor(.wmc_white_FFFFFF, for: .normal)
        btn.titleLabel?.font = 12.wmc_font
        btn.setBackgroundImage(UIColor.wmc_mainTheme.yp.uiImage, for: .normal)
        btn.layer.cornerRadius = 16
        btn.layer.masksToBounds = true
        return btn
    }()

    private var line: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH20_F7F7F7
        return view
    }()

    @objc
    private func customAction(_ sender: UIButton) {
        guard let poi = model?.poi else { return }
        
        let time = (WmcDateManager.shared.networkDate.value ?? Date()).timeIntervalSince1970
        WmcCustomAddressDBManager.insert(model: poi, createTime: time) { [weak self] isSuccess in
            guard let weakSelf = self else { return }
            if isSuccess {
                weakSelf.setCustomAddress?(weakSelf.index)
            }
        }
    }
}

